#include "precompile.h"
#include "base_refcounted.h"

_SSL_BEGIN
_SSBASEL_BEGIN

ref_counted_base::ref_counted_base()
{
	refcount_ = 1;
}

Atomic32 ref_counted_base::add_ref() const
{
	AtomicRefCountInc(&refcount_);
	return Acquire_Load(&refcount_);
}

Atomic32 ref_counted_base::release() const
{
	AtomicRefCountDec(&refcount_);
	Atomic32 ref_cnt = Acquire_Load(&refcount_);
	if(ref_cnt == 0)
		delete this;
	return ref_cnt;
}

bool ref_counted_base::has_one_ref() const
{
	return AtomicRefCountIsOne(
		&const_cast<ref_counted_base*>(this)->refcount_);
}

bool ref_counted_base::compare(ref_counted_interface *other) const
{
	return this == other;
}

#if 0
bool refcounted_thread_safe_base::has_one_ref() const
{
	return AtomicRefCountIsOne(
		&const_cast<refcounted_thread_safe_base*>(this)->refcount_);
}

refcounted_thread_safe_base::refcounted_thread_safe_base()
	: refcount_(0)
{

}

refcounted_thread_safe_base::~refcounted_thread_safe_base()
{

}

Atomic32 refcounted_thread_safe_base::add_ref() const
{
	AtomicRefCountInc(&refcount_);
	return Acquire_Load(&refcount_);
}

Atomic32 refcounted_thread_safe_base::release() const
{
	AtomicRefCountDec(&refcount_);
	return Acquire_Load(&refcount_);
}
#endif

_SSBASEL_END
_SSL_END


